package com.lanhe.admin.controller.withdrawal;

import com.lanhe.common.annotation.Log;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.enums.BusinessType;
import com.lanhe.common.utils.poi.ExcelUtil;
import com.lanhe.order.domain.LhOrdersWithdrawal;
import com.lanhe.order.service.ILhOrdersWithdrawalService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户提现控制器
 *
 * @author zhanggl
 * @since 2024年01月05日21:40:18
 */
@RestController
@RequestMapping("lhOrdersWithdrawal")
public class ILhOrdersWithdrawalController extends BaseController {

	/**
	 * 服务对象
	 */
	@Resource
	private ILhOrdersWithdrawalService lhOrdersWithdrawalService;

	/**
	 * 分页查询所有数据
	 */
	@PreAuthorize("@ss.hasPermi('withdrawal:LhUserController:list')")
	@GetMapping("/list")
	public TableDataInfo list(LhOrdersWithdrawal lhOrdersWithdrawal) {
		startPage();
		List<LhOrdersWithdrawal> lists = lhOrdersWithdrawalService.selectOrdersWithdrawalList(lhOrdersWithdrawal);
		for (LhOrdersWithdrawal list : lists) {
			list.setAuditStatusName(Constants.OrdersWithdrawalStatus.getOrdersWithdrawalStatusName(list.getAuditStatus()));
			list.setPayedName(Constants.OrdersPayedStatus.getOrdersPayedStatusName(list.getPayed()));
		}
		return getDataTable(lists);
	}

	/**
	 * 根据ID查询订单
	 *
	 * @param orderId id
	 * @author zhanggl
	 * @date 2024年01月05日22:02:11
	 */
	@PreAuthorize("@ss.hasPermi('withdrawal:LhUserController:query')")
	@GetMapping(value = {"/", "/{orderId}"})
	public AjaxResult getInfo(@PathVariable(value = "orderId") Long orderId) {
		LhOrdersWithdrawal lhOrdersWithdrawal = lhOrdersWithdrawalService.selectOrdersWithdrawalById(orderId);
		lhOrdersWithdrawal.setAuditStatusName(Constants.OrdersWithdrawalStatus.getOrdersWithdrawalStatusName(lhOrdersWithdrawal.getAuditStatus()));
		lhOrdersWithdrawal.setAuditStatusName(Constants.OrdersPayedStatus.getOrdersPayedStatusName(lhOrdersWithdrawal.getPayed()));
		return success(lhOrdersWithdrawal);
	}

	/**
	 * 审核
	 *
	 * @author zhanggl
	 * @date 2024年01月05日22:02:16
	 */
	@PreAuthorize("@ss.hasPermi('withdrawal:LhUserController:audit')")
	@PostMapping("/audit")
	public AjaxResult audit(@RequestBody LhOrdersWithdrawal ordersWithdrawal) {
		if (ObjectUtils.isEmpty(ordersWithdrawal.getOrderId())){
			return error("主键ID不能为空");
		}
		return toAjax(lhOrdersWithdrawalService.auditOrdersWithdrawalById(ordersWithdrawal));
	}

	/**
	 * 设为已付款
	 *
	 * @author zhanggl
	 * @date 2024年01月05日22:35:58
	 */
	@PreAuthorize("@ss.hasPermi('withdrawal:LhUserController:setPayed')")
	@PostMapping("/setPayed")
	public AjaxResult setPayed(@RequestBody LhOrdersWithdrawal ordersWithdrawal) {
		if (ObjectUtils.isEmpty(ordersWithdrawal.getOrderId())){
			return error("主键ID不能为空");
		}
		return toAjax(lhOrdersWithdrawalService.setPayed(ordersWithdrawal));
	}

	@PostMapping("/setWeiXinPayed")
	public AjaxResult setWeiXinPayed(@RequestBody Long[] ids) {
//		if (ObjectUtils.isEmpty(ordersWithdrawal.getOrderId())){
//			return error("主键ID不能为空");
//		}
//		return toAjax(lhOrdersWithdrawalService.setPayed(ordersWithdrawal));
//		for (Long id : ids) {
//			System.err.println("id "+id);
//		}
		return toAjax(lhOrdersWithdrawalService.setWeiXinPayed(ids));
	}



	/**
	 * 导出提现订单
	 */
	@Log(title = "提现订单", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, LhOrdersWithdrawal lhOrdersWithdrawal)
	{
		System.err.println("lhOrdersWithdrawal查询内容 "+ lhOrdersWithdrawal);
		List<LhOrdersWithdrawal> lists = lhOrdersWithdrawalService.selectOrdersWithdrawalList(lhOrdersWithdrawal);
		System.err.println("list "+lists);
		ExcelUtil<LhOrdersWithdrawal> util = new ExcelUtil<LhOrdersWithdrawal>(LhOrdersWithdrawal.class);
		util.exportExcel(response, lists, "用户提现表","支付宝批量付款文件");
	}
}

